#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>

#include "font.h"
#include "parameters.h"
#include "sdl_data.h"
#include "kill.h"
#include "dpi.h"
#include "music.h"

typedef enum {
	OPT_NONE,
	OPT_HIDE,
	OPT_EXIT
} mouse_option;

static mouse_option current_option = OPT_NONE;

void control_init() {
	glEnable(GL_TEXTURE_2D);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glViewport(0, 0, dpi_xi(CONTROL_WIDTH), dpi_yi(CONTROL_HEIGHT));

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, dpi_xi(CONTROL_WIDTH), 0, dpi_yi(CONTROL_HEIGHT), -10, 10);

	glMatrixMode(GL_MODELVIEW);

	glClearColor(0.f, 0.f, 0.f, 0.f);
}

#define CONTROL_PANEL_TEXT "glassdrop | control panel"
#define HIDE_TEXT "hide"
#define EXIT_TEXT "exit"

void color_fade_focused() {
	glColor4f(0.1f, 0.0f, 0.6f, 1.f);
}

void color_fade_unfocused() {
	glColor4f(0.5f, 0.5f, 0.5f, 0.2f);
}

void color_text_focused() {
	glColor4f(0.4f, 0.5f, 1.f, 1.f);
}

void color_text_unfocused() {
	glColor4f(0.7f, 0.7f, 0.7f, 1.f);
}

int pause_highlighted = 0;
int hide_highlighted = 0;
int close_highlighted = 0;

extern float control_panel_needed;

static void update_mouse() {
	int x, y;
	SDL_GetMouseState(&x, &y);
	y = dpi_y(C_SIZE) - y;

	int mouse_in_window = !!(SDL_GetWindowFlags(control_window) & SDL_WINDOW_MOUSE_FOCUS);

	pause_highlighted = 0;
	hide_highlighted = 0;
	close_highlighted = 0;

	if (mouse_in_window && y < dpi_y(C_SIZE)) {
		if(x < dpi_x(C_SIZE))
			pause_highlighted = 1;
		if (x > control_panel_needed && x < control_panel_needed + dpi_x(C_SIZE))
			hide_highlighted = 1;
		if (x > control_panel_needed + dpi_x(C_SIZE) && x < control_panel_needed + dpi_x(C_SIZE * 2))
			close_highlighted = 1;
	}

	/*y = dpi_y(CONTROL_HEIGHT) - y;
	if(flag || x < 0 || x > dpi_x(CONTROL_WIDTH) || y < 0 || y > dpi_y(40))
		current_option = OPT_NONE;
	else {
		if (x < dpi_x(CONTROL_WIDTH * 0.5f)) {
			current_option = OPT_HIDE;
		}
		else {
			current_option = OPT_EXIT;
		}
	}*/
}

void control_render() {
	/*glClear(GL_COLOR_BUFFER_BIT);

	void(*color_fade)();
	void(*color_text)();
	if (SDL_GetWindowFlags(control_window) & SDL_WINDOW_INPUT_FOCUS) {
		color_fade = color_fade_focused;
		color_text = color_text_focused;
	}
	else {
		color_fade = color_fade_unfocused;
		color_text = color_text_unfocused;
		current_option = OPT_NONE;
	}*/

	update_mouse();
	/*
	glBegin(GL_QUADS);
		glColor4f(0.f, 0.f, 0.f, 0.2f);
		glVertex2f(0.f, dpi_y(CONTROL_HEIGHT / 2.f));
		glVertex2f(dpi_x(CONTROL_WIDTH), dpi_y(CONTROL_HEIGHT / 2.f));

		color_fade();
		glVertex2f(dpi_x(CONTROL_WIDTH), dpi_y(CONTROL_HEIGHT));
		glVertex2f(0.f, dpi_y(CONTROL_HEIGHT));

		glColor4f(0.f, 0.f, 0.f, 0.2f);
		glVertex2f(0.f, dpi_y(CONTROL_HEIGHT / 2.f));
		glVertex2f(dpi_x(CONTROL_WIDTH), dpi_y(CONTROL_HEIGHT / 2.f));

		color_fade();
		glVertex2f(dpi_x(CONTROL_WIDTH), 0);
		glVertex2f(0.f, 0);
	glEnd();

	float title_x;
	float title_y;
	font_measure(CONTROL_PANEL_TEXT, &title_x, &title_y);

	title_x = 0.5f * (dpi_x(CONTROL_WIDTH) - title_x);
	title_y = dpi_y(CONTROL_HEIGHT) - 1.2f * title_y;
	
	color_text();
	font_draw(CONTROL_PANEL_TEXT, title_x, title_y);

	glBegin(GL_LINE_STRIP);
		glColor4f(0.f, 0.f, 0.f, 0.f);
		glVertex2f(0.f, dpi_y(CONTROL_HEIGHT));

		color_fade();
		glVertex2f(0.f, 0.f);
		glVertex2f(dpi_x(CONTROL_WIDTH), 0.f);

		glColor4f(0.f, 0.f, 0.f, 0.f);
		glVertex2f(dpi_x(CONTROL_WIDTH), dpi_y(CONTROL_HEIGHT));
	glEnd();

	if(current_option == OPT_HIDE) glColor4f(0.5f, 0.95f, 0.95f, 1.f);
	else color_text();
	font_draw(HIDE_TEXT, dpi_x(10), dpi_y(10));

	font_measure(EXIT_TEXT, &title_x, &title_y);
	if (current_option == OPT_EXIT) glColor4f(0.5f, 0.95f, 0.95f, 1.f);
	else color_text();
	font_draw(EXIT_TEXT, dpi_x(CONTROL_WIDTH - 10) - title_x, dpi_y(10));*/
}

void control_mouse_down() {
	/*if (current_option == OPT_HIDE) {
		SDL_MinimizeWindow(control_window);
	}
	else if (current_option == OPT_EXIT) {
		kill(0);
	}*/
	if (pause_highlighted) {
		toggle_music();
	}
	if (hide_highlighted) {
		SDL_MinimizeWindow(control_window);
	}
	if (close_highlighted) {
		kill(0);
	}
}